package org.jader.bolao.beans;

import java.util.HashSet;
import java.util.Set;


public class PropertyChanger {

	private Set<PropertyChangeListener> propertyListeners = new HashSet<PropertyChangeListener>();

	public void addPropertyChangeListener(PropertyChangeListener l) {
		checkPropertyListeners();
		if (!this.propertyListeners.contains(l))
			this.propertyListeners.add(l);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener l) {
		checkPropertyListeners();
		this.propertyListeners.remove(l);
	}
	
	public void firePropertyChange(Object source, String propertyName) {
		checkPropertyListeners();
		Object[] pcls = propertyListeners.toArray();
		for (int i=0; i<pcls.length; i++) ((PropertyChangeListener) pcls[i]).PropertyChanged(source, propertyName);
	}
	
	private void checkPropertyListeners() {
		if (this.propertyListeners == null) this.propertyListeners = new HashSet<PropertyChangeListener>();
	}

	protected void refreshPropertyChangeListeners() {}
}
